package net;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {

	public static void main(String[] args) {
		try {
			//127.0.0.1  或  localhost
			Socket socket=new Socket("192.168.1.104", 8877);
			System.out.println("我是客户机，已经和服务器"+socket.getInetAddress().getHostAddress());
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					while(true){
						try {
							ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
							String mess=(String) ois.readObject();
							System.out.println("===>"+mess);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (ClassNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}						
					}
				}
			}).start();
			Scanner kb=new Scanner(System.in);
			String mess=kb.nextLine();
			while(!mess.equals("bye")){
				ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
				oos.writeObject(mess);
				oos.flush();
				mess=kb.nextLine();
			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
